<template>
	<view class="post-item p-30 border-b">
		<!-- 帖子标题 -->
		<view class="text-30 font-bold mb-20" @click="goToDetail">
			{{ item.title }}
		</view>


		<!-- 内容预览 -->
		<view class="text-gray-600 text-28 mb-20 line-clamp-2">
			{{ item.content }}
		</view>
		<!-- 操作按钮（仅限自己） -->
		<view v-if="isMyPost" class="flex justify-end space-x-20">
			<view 
				class="btn-edit text-sm px-20 py-5 rounded bg-blue-100 text-blue-600"
				@click="editPost"
			>编辑</view>
			<view 
				class="btn-delete text-sm px-20 py-5 rounded bg-red-100 text-red-600"
				@click="deletePost"
			>删除</view>
		</view>
	</view>
</template>

<script>
import { PostApi } from '@/api/index.js';
	
export default {
	props: {
		item: {
			type: Object,
			required: true
		},
		currentUserId: {
			type: [String, Number],
			required: true
		}
	},
	computed: {
		isMyPost() {
			return this.item.userId == this.currentUserId;
		}
	},
	methods: {
		goToDetail() {
			uni.navigateTo({
				url: `/pages/home/detail?postId=${this.item.postId}`
			});
		},
		goToProfile() {
			if (this.item.userId != this.currentUserId) {
				uni.navigateTo({
					url: `/pages/paper/friend_profile?userId=${this.item.userId}`
				});
			}
		},
		editPost() {
			uni.navigateTo({
				url: `/pages/home/edit?postId=${this.item.postId}`
			});
		},
		deletePost() {
			uni.showModal({
				title: '提示',
				content: '确定要删除该帖子？',
				success: async (res) => {
					if (res.confirm) {
						try {
							await PostApi.deletePost(this.item.postId);
							this.$emit('deleted', this.item.postId); // 通知父组件刷新
							uni.showToast({ title: '删除成功' });
						} catch (err) {
							uni.showToast({ title: '删除失败', icon: 'none' });
						}
					}
				}
			});
		}
	}
};
</script>

<style lang="scss" scoped>
.btn-edit, .btn-delete {
	font-size: 24rpx;
}
.line-clamp-2 {
	display: -webkit-box;
	-webkit-line-clamp: 2;
	-webkit-box-orient: vertical;
	overflow: hidden;
}
</style>